package display.screens.faq
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import gs.TweenLite;
	
	import org.ilovecode.events.FaqEvent;
	import org.ilovecode.mvc.View;
	
	public class FaqItem extends View
	{
		protected var _questionTextField:TextField;
		protected var _answerTextField:TextField;
		protected var _contentMask:MovieClip;
		
		protected var _id:int;
		protected var _question:String;
		protected var _answer:String;
		protected var _isOpen:Boolean;
		
		protected var _minHeight:Number;
		protected var _maxHeight:Number;
		
		public function FaqItem(id:int = -1, question:String = "", answer:String = "", name:String="")
		{
			super(name);
			
			_id = id;
			_question = question;
			_answer = answer;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			this.useHandCursor = true;
			
			_questionTextField = TextField(this.getChildByName("question_txt"));
			_answerTextField = TextField(this.getChildByName("answer_txt"));
			_contentMask = MovieClip(this.getChildByName("contentMask"));
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			_answerTextField.autoSize = TextFieldAutoSize.LEFT;
			_answerTextField.multiline = true;
			_answerTextField.wordWrap = true;
		}
		
		override protected function initText():void
		{
			super.initText();
			
			_questionTextField.htmlText = _id.toString() + ". " + _question;
			_answerTextField.htmlText = _answer;
			
			_minHeight = _questionTextField.y + _questionTextField.textHeight;
			_maxHeight = _answerTextField.y + _answerTextField.textHeight;
			
			_contentMask.height = _minHeight;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			this.addEventListener(MouseEvent.CLICK, handle_MouseAction);
			model.addEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			this.removeEventListener(MouseEvent.CLICK, handle_MouseAction);
			model.removeEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case FaqEvent.SWITCH_ITEM:
					handle_SwitchItem(FaqEvent(event).id, FaqEvent(event).offset);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		protected function handle_SwitchItem(id:int, offset:Number):void
		{
			if (_id > id)
			{
				TweenLite.to(this, 0.5, {y:(this.y + offset)} );
			}
		}		
		
		
		protected function handle_MouseAction(event:MouseEvent):void
		{
			switch(event.type)
			{
				case MouseEvent.CLICK:
					handle_Click();
					break;
				default:
					break;
			}
		}
		
		protected function handle_Click():void
		{
			_isOpen = !_isOpen;
			
			var newHeight:Number = (_isOpen == true) ? _maxHeight : _minHeight;
			var offset:Number = (newHeight - _contentMask.height);
			
			TweenLite.to(_contentMask, 0.5, {height:newHeight} );
			controller.switchFaqItem(_id, offset);
		}

		public function get minHeight():Number
		{
			return _minHeight;
		}

		public function get maxHeight():Number
		{
			return _maxHeight;
		}


	}
}